<template>
  <div>
    <vxe-modal v-model="isVisiable" width="45vw" show-footer :mask="false">
      <template #title>
        <span class="text-bold">申请录入</span>
      </template>
      <template #default>
        <vxe-form title-colon ref="formRef" title-align="left" title-width="80" :data="formData" :loading="loading">
          <vxe-form-gather span="12">
            <vxe-form-item title="病人类型" field="patType" span="24">
              <template #default="params">
                <VxeDictSelection :url="'/api/sys/basDictDetail/findDictDetailList'" @confirmClick="dropSelectConfirm" :displayTexts="['name']" :displayTextObj="params.data.patType" :tableColumn="[{ field: 'code', title: '编码', width: `60px` }, { field: 'name', title: '名称', minWidth: `80px` }]" :aimKeys="[{ aimKey: 'patType', rowKey: 'code' }]" :dataObj="{ code: 'PATTYPE' }" />
              </template>
            </vxe-form-item>
            <vxe-form-item title="病历号" span="24" field="patNo">
              <template #default="params">
                <vxe-input v-model="params.data.patNo" placeholder="病历号"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="姓名" field="patNa" span="24" :item-render="{}" title-overflow>
              <template #default="params">
                <vxe-input v-model="params.data.patNa" placeholder="姓名"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="性别" field="sex" span="24" :item-render="{}" title-overflow="title">
              <template #default="params">
                <VxeDictSelection :url="'/api/sys/basDictDetail/findDictDetailList'" @confirmClick="dropSelectConfirm" :displayTexts="['name']" :displayTextObj="params.data.sex" :tableColumn="[{ field: 'code', title: '编码', width: `60px` }, { field: 'name', title: '名称', minWidth: `80px` }]" :aimKeys="[{ aimKey: 'sex', rowKey: 'code' }]" :dataObj="{ code: 'sex' }" />
              </template>
            </vxe-form-item>
            <vxe-form-item title="年龄" field="age" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #default="params">
                <!-- <vxe-input v-model="params.data.age" placeholder="年龄"></vxe-input> -->
                <AgeComp :ageObj="{ age: params.data['age'], ageUnit: params.data['ageUnit'], secAge: params.data['secAge'], secAgeUnit: params.data['secAgeUnit'] }" @ageChange="ageChange" />
              </template>
            </vxe-form-item>
            <vxe-form-item title="联系电话" field="telNo" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #default="params">
                <vxe-input v-model="params.data.telNo" placeholder="联系电话"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="临床诊断" field="diagnose" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #default="params">
                <vxe-input v-model="params.data.diagnose" placeholder="临床诊断"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="采集时间" field="samplingTime" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #default="params">
                <vxe-input v-model="params.data.samplingTime" type="date" placeholder="采集时间" transfer></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="采集部位" field="remark2" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #default="params">
                <vxe-input v-model="params.data.remark2" placeholder="采集部位"></vxe-input>
              </template>
            </vxe-form-item>
          </vxe-form-gather>
          <vxe-form-gather span="12">
            <vxe-form-item title="样本号" field="remark3" span="24">
              <template #title>
                <span class="padding-lr-xs">样本号</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.remark3" placeholder="样本号" disabled></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="住院次数" span="24" field="remark4">
              <template #title>
                <span class="padding-lr-xs">住院次数</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.remark4" placeholder="住院次数"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="科室" field="reqDeptNa" span="24" :item-render="{}" title-overflow>
              <template #title>
                <span class="padding-lr-xs">科室</span>
              </template>
              <template #default="params">
                <VxePulldownSelectionWithNa :url="'/api/sys/findDepartmentListbyCurrentUserOrg'" @confirmClick="dropPullDownConfirm" :tableColumn="[{ field: 'code', title: '编码', width: '60px' }, { field: 'name', title: '名称' }]" :aimKeys="[{ aimKey: 'reqDept', rowKey: 'code' }, { aimKey: 'reqDeptNa', rowKey: 'name' }]" :displayTexts="['name']" :displayTextObj="params.data.reqDeptNa ? params.data.reqDeptNa : ''" />
              </template>
            </vxe-form-item>
            <vxe-form-item title="病区" field="reqWard" span="24" :item-render="{}" title-overflow="title">
              <template #title>
                <span class="padding-lr-xs">病区</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.reqWard" placeholder="病区"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="床号" field="bedNo" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #title>
                <span class="padding-lr-xs">床号</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.bedNo" placeholder="床号"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="费别" field="feeType" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #title>
                <span class="padding-lr-xs">费别</span>
              </template>
              <template #default="params">
                <VxeDictSelection :url="'/api/sys/basDictDetail/findDictDetailList'" @confirmClick="dropSelectConfirm" :displayTexts="['name']" :displayTextObj="params.data.name" :tableColumn="[{ field: 'code', title: '编码', width: `60px` }, { field: 'name', title: '名称', minWidth: `80px` }]" :aimKeys="[{ aimKey: 'feeType', rowKey: 'code' }]" :dataObj="{ code: 'CostType' }" />
              </template>
            </vxe-form-item>
            <vxe-form-item title="申请医生" field="reqDoctor" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #title>
                <span class="padding-lr-xs">申请医生</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.reqDoctor" placeholder="申请医生"></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="签收时间" field="signTime" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #title>
                <span class="padding-lr-xs">签收时间</span>
              </template>
              <template #default="params">
                <vxe-input v-model="params.data.signTime" type="date" placeholder="签收时间" transfer></vxe-input>
              </template>
            </vxe-form-item>
            <vxe-form-item title="标本类型" field="sampleTypeNa" span="24" :item-render="{}" title-overflow="ellipsis">
              <template #title>
                <span style="color: red;" class="padding-lr-xs">标本类型</span>
              </template>
              <template #default="params">
                <VxePulldownSelectionWithNa :url="'/api/lab/labSampleType/findSampleTypeList'" @confirmClick="dropPullDownConfirmSampleType" :tableColumn="[{ field: 'sampleTypeCode', title: '编码', width: '60px' }, { field: 'sampleTypeName', title: '名称' }]" :aimKeys="[{ aimKey: 'sampleType', rowKey: 'sampleTypeCode' }, { aimKey: 'sampleTypeNa', rowKey: 'sampleTypeName' }]" :displayTexts="['sampleTypeName']" :displayTextObj="params.data.sampleTypeNa ? params.data.sampleTypeNa : ''" />
              </template>
            </vxe-form-item>
          </vxe-form-gather>
          <vxe-form-gather span="24">
            <vxe-form-item title="检验目的" field="reqPurpose" span="24" :item-render="{}">
              <template #title>
                <span style="color: red;">检验目的</span>
              </template>
              <template #default="params">
                <VxePulldownSelectionWithNa :url="'/api/bac/bacReqPurpose/findPurposeList'" @confirmClick="dropPullDownConfirmReqPurpose" :tableColumn="[{ field: 'purposeNo', title: '编码', width: '60px' }, { field: 'purposeName', title: '名称' }]" :aimKeys="[{ aimKey: 'purposeNo', rowKey: 'purposeNo' }, { aimKey: 'reqPurpose', rowKey: 'purposeName' }]" :displayTexts="['purposeName']" :displayTextObj="params.data.reqPurpose ? params.data.reqPurpose : ''" />
              </template>
            </vxe-form-item>
          </vxe-form-gather>
          <vxe-form-gather span="24">
            <vxe-form-item title="标本备注" field="remark1" span="24" :item-render="{}">
              <template #default="params">
                <vxe-textarea v-model="params.data.remark1" placeholder="标本备注" :autosize="{ minRows: 4, maxRows: 6 }" clearable></vxe-textarea>
              </template>
            </vxe-form-item>
          </vxe-form-gather>
        </vxe-form>
      </template>
      <template #footer>
        <vxe-button type="text" class="button-white margin-right" @click="cancelEvent">关闭</vxe-button>
        <vxe-button type="text" class="button-blue" @click="confirmEvent">确定</vxe-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, getCurrentInstance, defineProps } from "vue";
import { Splitpanes, Pane } from 'splitpanes';
import { formatDateTime, IsEmpty } from '@/utils/common';
import VxeDictSelection from '@/components/VxeDictSelection.vue';
import VxePulldownSelectionWithNa from '@/components/VxePulldownSelectionWithNa.vue';
import AgeComp from '@/components/AgeComp.vue';
import { VXETable } from 'vxe-table';

let { proxy }: any = getCurrentInstance();
let isVisiable = ref(false);
let changeST = ref(-1);/**改变的样本类型、检验目的【-1:无改变、1:样本类型】 */
let changeRP = ref(-1);/**改变的检验目的、检验目的【-1:无改变、1:检验目的】 */
let orgSampleType = ref('');
let orgSampleTypeNa = ref('');
let orgReqPurposeId = ref('');
let orgReqPurpose = ref('');
let isNew = ref(true);
const formRef = ref(null);

const loading = ref(false)

const formData = reactive({
  reqNo: '',
  state: '',
  stateNa: '',
  isCharge: 0,
  isUrgent: 0,
  reqPurposeId: '',
  reqPurpose: '',
  testPurpose: '',
  sampleType: '',
  sampleTypeNa: '',
  reqType: '',
  feeType: '',/**费别 */
  patType: '',
  patNo: '',
  patNa: '',
  sex: '',
  age: 0,
  ageUnit: '',
  birthDay: null,
  ageStr: '',
  idType: '',
  idNo: '',
  patId: '',
  sin: '',
  visitNo: '',
  telNo: '',
  diagnose: '',
  country: '',
  province: '',
  city: '',
  district: '',
  subdistrict: '',
  address: '',
  bedNo: '',
  nurser: '',
  nurserNa: '',
  phyCycel: '',
  lactation: '',
  gestation: '',
  gestationWeek: '',
  gestationTimes: '',
  gestationStage: '',
  reqWard: '',
  reqDept: '',
  reqDeptNa: '',
  reqDoctor: '',
  reqDoctorNa: '',
  reqTime: null,
  isPrint: 0,
  printDoctor: '',
  printerNa: '',
  printTime: null,
  isSampling: 0,
  samplingDoctor: '',
  samplingDoctorNa: '',
  samplingTime: null,
  samplingDept: '',
  samplingDeptNa: '',
  isSign: 1,
  signerDoctor: '',
  signerNa: '',
  signTime: formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss'),
  isCancel: 0,
  cancelDoctor: '',
  cancelDoctorNa: '',
  cancelTime: null,
  cancelReason: '',
  patPyName: '',
  barCode: '',
  isManual: 0,
  remark1: '',
  remark2: '',
  remark3: '',
  remark4: ''
})

const formRules = ref({
  name: [
    { required: true, message: '请输入名称' },
    { min: 3, max: 5, message: '长度在 3 到 5 个字符' }
  ],
  sex: [
    { required: true, message: '请选择性别' }
  ],
  age: [
    { required: true, message: '请输入年龄' },
    {
      validator({ itemValue }) {
        // 自定义校验
        if (Number(itemValue) > 35 || Number(itemValue) < 18) {
          return new Error('年龄在 18 ~ 35 之间')
        }
      }
    }
  ],
  date: [
    { required: true, message: '必填校验' }
  ]
})

defineExpose({
  showModal,/**打开选择方案的弹窗 */
  editShowModal
});

/**页面初始化/切换显示隐藏 */
function showModal() {
  isNew.value = true;
  changeST.value = -1;
  orgSampleType.value = '';
  orgSampleTypeNa.value = '';
  orgReqPurposeId.value = '';
  orgReqPurpose.value = '';
  changeRP.value = -1;
  isVisiable.value = true;
  resetForm();
}

/**编辑申请初始化 */
function editShowModal(obj) {
  isNew.value = false;
  orgSampleType.value = obj.sampleType;
  orgSampleTypeNa.value = obj.sampleTypeNa;
  orgReqPurposeId.value = obj.reqPurposeId;
  orgReqPurpose.value = obj.reqPurpose;
  changeST.value = -1;
  changeRP.value = -1;
  isVisiable.value = true;
  Object.assign(formData, obj);
}

/**重置form表单 */
function resetForm() {
  formRef.value?.reset();
  formData.reqNo = '';
  formData.state = '';
  formData.stateNa = '';
  formData.isCharge = 0;
  formData.isUrgent = 0;
  formData.reqPurposeId = '';
  formData.reqPurpose = '';
  formData.testPurpose = '';
  formData.sampleType = '';
  formData.sampleTypeNa = '';
  formData.reqType = '';
  formData.feeType = '';/**费别 */
  formData.patType = '';
  formData.patNo = '';
  formData.patNa = '';
  formData.sex = '';
  formData.age = 0;
  formData.ageUnit = '';
  formData.birthDay = null;
  formData.ageStr = '';
  formData.idType = '';
  formData.idNo = '';
  formData.patId = '';
  formData.sin = '';
  formData.visitNo = '';
  formData.telNo = '';
  formData.diagnose = '';
  formData.country = '';
  formData.province = '';
  formData.city = '';
  formData.district = '';
  formData.subdistrict = '';
  formData.address = '';
  formData.bedNo = '';
  formData.nurser = '';
  formData.nurserNa = '';
  formData.phyCycel = '';
  formData.lactation = '';
  formData.gestation = '';
  formData.gestationWeek = '';
  formData.gestationTimes = '';
  formData.gestationStage = '';
  formData.reqWard = '';
  formData.reqDept = '';
  formData.reqDeptNa = '';
  formData.reqDoctor = '';
  formData.reqDoctorNa = '';
  formData.reqTime = null;
  formData.isPrint = 0;
  formData.printDoctor = '';
  formData.printerNa = '';
  formData.printTime = null;
  formData.isSampling = 0;
  formData.samplingDoctor = '';
  formData.samplingDoctorNa = '';
  formData.samplingTime = null;
  formData.samplingDept = '';
  formData.samplingDeptNa = '';
  formData.isSign = 1;
  formData.signerDoctor = '';
  formData.signerNa = '';
  formData.signTime = formatDateTime(new Date(), 'yyyy-MM-dd hh:mm:ss');
  formData.isCancel = 0;
  formData.cancelDoctor = '';
  formData.cancelDoctorNa = '';
  formData.cancelTime = null;
  formData.cancelReason = '';
  formData.patPyName = '';
  formData.barCode = '';
  formData.isManual = 0;
  formData.remark1 = '';
  formData.remark2 = '';
  formData.remark3 = '';
  formData.remark4 = '';
}

/**确认选择 */
async function confirmEvent() {
  let content = '';
  let isChange = false;
  if (changeST.value > -1 && changeRP.value > -1 && !isNew.value) {
    isChange = true;
    content = `[${orgSampleTypeNa.value} 改为 ${formData.sampleTypeNa}];\r\n[${orgReqPurpose.value} 改为 ${formData.reqPurpose}]\r\n将会重新引用申请项目、选用培养方案、培养基等,是否继续?`;
  }
  if (changeST.value > -1 && changeRP.value === -1 && !isNew.value) {
    isChange = true;
    content = `[${orgSampleTypeNa.value} 改为 ${formData.sampleTypeNa}]\r\n将会重新引用申请项目、选用培养方案、培养基等,是否继续?`;
  }
  if (changeRP.value > -1 && changeST.value === -1 && !isNew.value) {
    isChange = true;
    content = `[${orgReqPurpose.value} 改为 ${formData.reqPurpose}]\r\n将会重新引用申请项目、选用培养方案、培养基等,是否继续?`;
  }
  if (isChange) {
    const type = await VXETable.modal.confirm(content);
    if (type === 'confirm') {
      proxy.$emit('confirmSaveReset', formData);
    }
    isVisiable.value = false;
    return;
  }


  proxy.$emit('confirmSave', formData);
  isVisiable.value = false;
}
/**关闭窗体 */
function cancelEvent() {
  isVisiable.value = false;
}
/**下拉选中 */
function dropPullDownConfirm(e) {
  e.aimKeys.forEach(m => {
    formData[m.aimKey] = e.row[m.rowKey];
  });
}
/**下拉选中(样本类型) */
function dropPullDownConfirmSampleType(e) {
  if (e.row.sampleTypeCode !== orgSampleType.value) {
    changeST.value = 1;
  } else {
    changeST.value = -1;
  }
  e.aimKeys.forEach(m => {
    formData[m.aimKey] = e.row[m.rowKey];
  });
}
/**下拉选中 */
function dropSelectConfirm(e) {
  e.aimKeys.forEach(m => {
    formData[m.aimKey] = e.row[m.rowKey];
  });
}
/**下拉选中(检验目的) */
function dropPullDownConfirmReqPurpose(e) {
  if (e.row.id !== orgReqPurposeId.value) {
    changeRP.value = 1;
  } else {
    changeRP.value = -1;
  }
  e.aimKeys.forEach(m => {
    formData[m.aimKey] = e.row[m.rowKey];
  });
}
/**年龄改变 */
function ageChange(ageObj) {
  Object.assign(formData, ageObj);
}
/** */
</script>

<style scoped lang="scss">
:deep(.vxe-input--inner) {
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
}

:deep(.vxe-textarea--inner) {
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
}

:deep(.vxe-button.type--button:not(.is--round)) {
  border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
}
</style>
